package thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author Han
 * 线程屏障,当到一定状态后线程才能执行，countdownlatch是一定线程完成后，才继续执行。
 */
public class CyclicBarrierDemo {
	static class Work extends Thread{
		CyclicBarrier barrier;
		public Work(CyclicBarrier barrier){
			this.barrier = barrier;
		}
		public void run(){
			System.out.println("start...");
			try {
				barrier.await();
			} catch (InterruptedException | BrokenBarrierException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("ok,over");
		}
	}
	
	public static void main(String[] args) {
		CyclicBarrier barrier = new CyclicBarrier(5);
		Thread thread1 = new Work(barrier);
		Thread thread2 = new Work(barrier);
		Thread thread3 = new Work(barrier);
		Thread thread4 = new Work(barrier);
		thread1.start();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		thread2.start();
		thread3.start();
		thread4.start();
		System.out.println("...");
	}
}
